classdef dotsDrawableText < dotsDrawable
    % @class dotsDrawableText
    % Display a string graphically.
    % @details
    % Properties like font font size apply to the whole drawing window.
    % See dotsTheDrawablesManager to change them.
    %
    % @ingroup drawable
    
    properties (SetObservable = true)
        % x-coordinate for the center of the text (degrees visual
        % angle, centered in window)
        x = 0;
        
        % y-coordinate for the center of the text (degrees visual
        % angle, centered in window)
        y = 0;
        
        % text string to be displayed
        string = '';
        
        % color of the displayed text
        color;
    end
    
    methods
        % Constructor takes no arguments.
        function self = dotsDrawableText
            self = self@dotsDrawable;
            self.color = self.foregroundColor;
        end

        % Draw the text string, centered on x and y.
        function draw(self)
            % ask Screen how big the text will be
            if ~isempty(self.string)
                boundingRect = Screen('TextBounds', ...
                    self.windowNumber, ...
                    self.string);
                
                % center the text on x and y
                xPix = self.pixelsPerDegree*self.x ...
                    + self.windowRect(3)/2 ....
                    - boundingRect(3)/2;
                yPix = -self.pixelsPerDegree*self.y ...
                    + self.windowRect(4)/2 ....
                    - boundingRect(4)/2;
                
                Screen('DrawText', ...
                    self.windowNumber, ...
                    self.string, ...
                    xPix, ...
                    yPix, ...
                    self.color, ...
                    self.backgroundColor);
            end
        end
    end
end